﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Drawing;
using svs.Mobile.BeyondMedia.UiCore.GDI;

namespace svs.Mobile.BeyondMedia.UiCore.KineticControls
{
	public class ListViewSettings
	{
		private BrushGdi _listBackgroundBrush;
		private BrushGdi _ListGroupBackground;
		private BrushGdi _listItemBackgroundOdd;
		private BrushGdi _listItemBackgroundEven;
		private BrushGdi _ListItemSeparator;
		private BrushGdi _BackgroundHeaderBrush;
		private PenGdi _BackgroundHeaderPen;

		private int _screenDPI = 0;

		//Sizes in Points
		public int ListItemHeight = 33;

		public int PrimaryTextIndent = 8;
		
		public int PrimaryIconWidth = 27;
		public int PrimaryIconHeight = 27;

		public int SecondaryIconHeight = 27;
		public int SecondaryIconWidth = 27;

		public int GroupItemIndent = 10;		
		public int GroupHeight = 13;

		public int FooterHeight = 0;

		public int ListItemContentMargin = 3;

		public int MarginLeft = 0;
		public int MarginTop = 0;
		public int MarginBottom = 0;
		public int MarginRight = 0;

		public int BackgroundHeaderHeight = 40;
		public int BackgroundHeaderMargin = 5;

		//Calculated sizes in Pixels
		internal int ListItemPixHeight;

		internal int PrimaryTextIndentPix;

		internal int PrimaryIconPixWidth;
		internal int PrimaryIconPixHeight;

		internal int SecondaryIconPixHeight;
		internal int SecondaryIconPixWidth;

		internal int GroupItemIndentPix;
		internal int GroupPixHeight;

		internal int FooterPixHeight;

		public int ListItemContentPixMargin;

		public int MarginPixLeft;
		public int MarginPixTop;
		public int MarginPixBottom;
		public int MarginPixRight;

		public int BackgroundHeaderPixHeight;
		public int BackgroundHeaderPixMargin;

		//Fonts and colors
		public string DefaultFontName = "Tahoma";
		
		//Font sizes are in logical points
		public int GroupFontSize = 10;
		public int ItemFontSize = 11;
		public int SecondaryItemFontSize = 9;
		public int NoteFontSize = 9;


		// Sizes in Pixels
		internal int GroupFontPixHeight;
		internal int ItemFontPixHeight;
		internal int SecondaryItemFontPixHeight;
		internal int NoteFontPixHeight;

		public Color ListBackgroundColor = Color.LightGray;

		public Color ListGroupBackgroundColor = Color.Gray;
		public Color ListGroupTextColor = Color.White;

		public Color ListItemTextColor = SystemColors.ControlText;
		public Color ListItemSelectedTextColor = SystemColors.ActiveCaptionText;
		public Color ListItemBackgroundColorOdd = SystemColors.Menu;
		public Color ListItemBackgroundColorEven = SystemColors.Menu;
		
		public Color ListItemSecondaryTextColor = Color.Gray;

		public Color ListNoteTextColor = SystemColors.ControlText;
		public Color ListNoteSelectedTextColor = SystemColors.ActiveCaptionText;

		public Color ItemHighLightColor1 = SystemColors.Highlight;
		public Color ItemHighLightColor2 = SystemColors.InactiveCaption;

		public Color ListItemSeparatorColor = Color.Gainsboro;

		public Color HeaderBackgroundColor = Color.LightBlue;
		public Color HeaderLineColor = Color.LightBlue;
		public Color HeaderTextColor = Color.LightBlue;
		
		public bool UseClearType;
    
		public BrushGdi ListBackgroundBrush
		{
			get
			{
				if (_listBackgroundBrush == null)
				{
					_listBackgroundBrush = Gdi.CreateSolidBrush(ListBackgroundColor);
				}
				return _listBackgroundBrush;
			}
		}

		public BrushGdi ListGroupBackground
		{
			get
			{
				if (_ListGroupBackground == null)
				{
					_ListGroupBackground = Gdi.CreateSolidBrush(ListGroupBackgroundColor);
				}
				return _ListGroupBackground;
			}
		}

		public BrushGdi ListItemSeparator
		{
			get
			{
				if (_ListItemSeparator == null)
				{
					_ListItemSeparator = Gdi.CreateSolidBrush(ListItemSeparatorColor);
				}
				return _ListItemSeparator;
			}
		}

		public BrushGdi ListItemBackgroundOdd
		{
			get
			{
				if (_listItemBackgroundOdd == null)
				{
					_listItemBackgroundOdd = Gdi.CreateSolidBrush(ListItemBackgroundColorOdd);
				}
				return _listItemBackgroundOdd;
			}
		}

		public BrushGdi ListItemBackgroundEven
		{
			get
			{
				if (_listItemBackgroundEven == null)
				{
					_listItemBackgroundEven = Gdi.CreateSolidBrush(ListItemBackgroundColorEven);
				}
				return _listItemBackgroundEven;
			}
		}

		public BrushGdi BackgroundHeaderBrush
		{
			get
			{
				if (_BackgroundHeaderBrush == null)
				{
					_BackgroundHeaderBrush = Gdi.CreateSolidBrush(HeaderBackgroundColor);
				}
				return _BackgroundHeaderBrush;
			}
		}

		public PenGdi BackgroundHeaderPen
		{
			get
			{
				if (_BackgroundHeaderPen == null)
				{
					_BackgroundHeaderPen = Gdi.CreatePen(HeaderLineColor);
				}
				return _BackgroundHeaderPen;
			}
		}

		public FontGdi PrimaryListFont
		{
			get { return FontCache.CreateFont(DefaultFontName, ItemFontPixHeight, FontStyle.Regular, UseClearType); }
		}

		public FontGdi PrimaryListFontBold
		{
			get { return FontCache.CreateFont(DefaultFontName, ItemFontPixHeight, FontStyle.Bold, UseClearType); }
		}

		public FontGdi SecondaryListFont
		{
			get { return FontCache.CreateFont(DefaultFontName, SecondaryItemFontPixHeight, FontStyle.Regular, UseClearType); }
		}

		public FontGdi GroupFont
		{
			get { return FontCache.CreateFont(DefaultFontName, GroupFontPixHeight, FontStyle.Bold, UseClearType); }
		}

		public FontGdi NoteFont
		{
			get { return FontCache.CreateFont(DefaultFontName, NoteFontPixHeight, FontStyle.Regular, UseClearType); }
		}

		public int ScreenDPI
		{
			get
			{
				return _screenDPI;
			}
		}

		public Size PrimaryIconPixSize
		{
			get
			{
				return new Size(PrimaryIconPixWidth, PrimaryIconPixHeight);
			}
		}

		public void AdjustScreenValuesForCurrentDPI()
		{
				ListItemPixHeight = Win32.MulDivEx(ListItemHeight, ScreenDPI, 72);

				FooterPixHeight = Win32.MulDivEx(FooterHeight, ScreenDPI, 72);

				MarginPixLeft = Win32.MulDivEx(MarginLeft, ScreenDPI, 72);
				MarginPixRight = Win32.MulDivEx(MarginRight, ScreenDPI, 72);
				MarginPixTop = Win32.MulDivEx(MarginTop, ScreenDPI, 72);
				MarginPixBottom = Win32.MulDivEx(MarginBottom, ScreenDPI, 72);

				BackgroundHeaderPixHeight = Win32.MulDivEx(BackgroundHeaderHeight, ScreenDPI, 72);
				BackgroundHeaderPixMargin = Win32.MulDivEx(BackgroundHeaderMargin, ScreenDPI, 72);

				ListItemContentPixMargin = Win32.MulDivEx(ListItemContentMargin, ScreenDPI, 72);

				PrimaryTextIndentPix = Win32.MulDivEx(PrimaryTextIndent, ScreenDPI, 72);

				PrimaryIconPixWidth = Win32.MulDivEx(PrimaryIconWidth, ScreenDPI, 72);
				PrimaryIconPixHeight = Win32.MulDivEx(PrimaryIconHeight, ScreenDPI, 72);

				SecondaryIconPixHeight = Win32.MulDivEx(SecondaryIconHeight, ScreenDPI, 72);
				SecondaryIconPixWidth = Win32.MulDivEx(SecondaryIconWidth, ScreenDPI, 72);

				GroupItemIndentPix = Win32.MulDivEx(GroupItemIndent, ScreenDPI, 72);
				GroupPixHeight = Win32.MulDivEx(GroupHeight, ScreenDPI, 72);

				GroupFontPixHeight = Win32.MulDivEx(GroupFontSize, ScreenDPI, 72);
				ItemFontPixHeight = Win32.MulDivEx(ItemFontSize, ScreenDPI, 72);
				SecondaryItemFontPixHeight = Win32.MulDivEx(SecondaryItemFontSize, ScreenDPI, 72);
				NoteFontPixHeight = Win32.MulDivEx(NoteFontSize, ScreenDPI, 72);
		}


		public ListViewSettings(int screenDPI)
		{
			_screenDPI = screenDPI;
		}

		public static ListViewSettings GetDefaultSettingsForPlatform(int screenDPI)
		{
			ListViewSettings retVal;

			if(PlatformDetection.IsPocketPC())
			{
				retVal = screenDPI == 192 ? Get192DPISettings(screenDPI) : new ListViewSettings(screenDPI);
			}
			else
			{
				retVal = Get131DPISettings(screenDPI);
			}

			retVal.AdjustScreenValuesForCurrentDPI();
			return retVal;
		}

		private static ListViewSettings Get131DPISettings(int screenDPI)
		{
			var retVal = new ListViewSettings(screenDPI)
			             	{
			             		ListItemHeight = 23,
			             		ListItemContentMargin = 0,
			             		NoteFontSize = 7,
			             		SecondaryItemFontSize = 8,
			             		BackgroundHeaderHeight = 33
			             	};

			retVal.PrimaryIconWidth = retVal.ListItemHeight;
			retVal.PrimaryIconHeight = retVal.ListItemHeight;
			retVal.SecondaryIconHeight = retVal.ListItemHeight;
			retVal.SecondaryIconWidth = retVal.ListItemHeight;
			
			return retVal;
		}

		static ListViewSettings Get192DPISettings(int screenDPI)
		{
			var retVal = new ListViewSettings(screenDPI)
			{
				ListItemHeight = 27,
				ListItemContentMargin = 2,
				NoteFontSize = 8,
				PrimaryTextIndent = 5,
				ItemFontSize = 10,
				SecondaryItemFontSize = 9
			};

			retVal.PrimaryIconHeight = retVal.ListItemHeight - (retVal.ListItemContentMargin * 2);
			retVal.PrimaryIconWidth = retVal.PrimaryIconHeight;
			retVal.SecondaryIconHeight = retVal.PrimaryIconHeight;
			retVal.SecondaryIconWidth = retVal.PrimaryIconHeight;

			return retVal;
		}
	}
}